package com.seafood.repository;

import com.seafood.entity.SmsVerificationCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 短信验证码Repository
 */
@Repository
public interface SmsVerificationCodeRepository extends JpaRepository<SmsVerificationCode, Long> {
    
    /**
     * 根据手机号和验证码类型查找最新的有效验证码
     */
    @Query("SELECT s FROM SmsVerificationCode s WHERE s.phone = :phone AND s.codeType = :codeType " +
           "AND s.isUsed = false AND s.expireTime > :now ORDER BY s.createdAt DESC")
    List<SmsVerificationCode> findValidCodesByPhoneAndType(@Param("phone") String phone, 
                                                          @Param("codeType") SmsVerificationCode.CodeType codeType,
                                                          @Param("now") LocalDateTime now);
    
    /**
     * 根据手机号、验证码和类型查找验证码
     */
    Optional<SmsVerificationCode> findByPhoneAndCodeAndCodeType(String phone, String code, SmsVerificationCode.CodeType codeType);
    
    /**
     * 查找指定手机号在特定时间段内发送的验证码数量
     */
    @Query("SELECT COUNT(s) FROM SmsVerificationCode s WHERE s.phone = :phone " +
           "AND s.codeType = :codeType AND s.createdAt >= :startTime")
    Long countByPhoneAndCodeTypeAndCreatedAtAfter(@Param("phone") String phone, 
                                                 @Param("codeType") SmsVerificationCode.CodeType codeType,
                                                 @Param("startTime") LocalDateTime startTime);
    
    /**
     * 根据IP地址统计发送次数
     */
    @Query("SELECT COUNT(s) FROM SmsVerificationCode s WHERE s.ipAddress = :ipAddress " +
           "AND s.createdAt >= :startTime")
    Long countByIpAddressAndCreatedAtAfter(@Param("ipAddress") String ipAddress, 
                                          @Param("startTime") LocalDateTime startTime);
    
    /**
     * 查找过期的验证码
     */
    @Query("SELECT s FROM SmsVerificationCode s WHERE s.expireTime < :now")
    List<SmsVerificationCode> findExpiredCodes(@Param("now") LocalDateTime now);
    
    /**
     * 删除过期的验证码
     */
    @Query("DELETE FROM SmsVerificationCode s WHERE s.expireTime < :cutoffTime")
    void deleteExpiredCodes(@Param("cutoffTime") LocalDateTime cutoffTime);
    
    /**
     * 根据手机号查找所有验证码（按创建时间倒序）
     */
    List<SmsVerificationCode> findByPhoneOrderByCreatedAtDesc(String phone);
    
    /**
     * 统计各类型验证码数量
     */
    @Query("SELECT s.codeType, COUNT(s) FROM SmsVerificationCode s " +
           "WHERE s.createdAt >= :startTime GROUP BY s.codeType")
    List<Object[]> countByCodeTypeAndCreatedAtAfter(@Param("startTime") LocalDateTime startTime);
    
    /**
     * 查找频繁发送验证码的手机号
     */
    @Query("SELECT s.phone, COUNT(s) as sendCount FROM SmsVerificationCode s " +
           "WHERE s.createdAt >= :startTime GROUP BY s.phone " +
           "HAVING COUNT(s) > :threshold ORDER BY sendCount DESC")
    List<Object[]> findFrequentSenders(@Param("startTime") LocalDateTime startTime, 
                                      @Param("threshold") Long threshold);
}